<?php

namespace co2_OS;

use co2\dev\v0_0_0 as api;

function install_lib($lib,$version)

{
  log::register("INSTALL_LIB: Received install signal for lib $lib version $version");
  
	if(aptitude::exists('lib',$lib,$version))
	
	{
	  log::register("INSTALL_LIB: Lib already installed");
	  
	  throw new api\Exception('Already installed',null,ES_LIB_ALREADY_INSTALLED);
	}

  log::register("INSTALL_LIB: Downloading lib from Amazon server");
  
  $curl=new api\curl('http://'.sysvar::get('s3-bucket').".s3.amazonaws.com/lib/$lib-$version.tar.gz");
  
  log::register("INSTALL_LIB: Download OK :)");
  
	$dl=$curl->getBody();
	
	$file=new api\File(sysvar::get('lib').'/'.$lib.'-'.$version.'.tar.gz');
	
	$tar=$file->put($dl);
	
	new api\bash\tar('x','z',$tar,(object)array('target'=>sysvar::get('lib')));
	
	unlink($tar);
	
	if(!$manifest=parse_manifest('lib',$lib,$version))
	throw new api\Exception('could not read manifest of lib '.$lib.' version '.$version);
	
	aptitude::add(aptitude::TYPE_LIB,$lib,$version);
	
	log::register("INSTALL_LIB: Installation complete :)");
}
